<?php
class ModelTotalTax extends Model {	
	public function getTax($product_id, $amount, $country_id = 0, $zone_id = 0) {
		$rate = $this->getTaxRate($product_id, $country_id, $zone_id);
		
		$tax = $amount * ($rate / 100);
		
		return $tax;
	}
	
	public function getTaxValue($amount, $rate) {
		$tax = $amount * ($rate / 100);
		
		return $tax;
	}
	
	public function getTaxRate($product_id, $country_id = 0, $zone_id = 0){
		$rate = 0;	
		
		$sql_product = "SELECT product_id, tax_class_id FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'";
		$query_product = $this->db->query($sql_product);	
		$tax_class_id = $query_product->row ? $query_product->row['tax_class_id'] : 0;
	
		if($tax_class_id > 0){
			$sql = "SELECT tr.tax_class_id, tr.rate AS rate, tr.description, tr.priority " .
				" FROM " . DB_PREFIX . "tax_rate tr " .
				" LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr.geo_zone_id = z2gz.geo_zone_id) " .
				" LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr.geo_zone_id = gz.geo_zone_id) " .
				" WHERE tr.tax_class_id = '" . (int)$tax_class_id . "' AND (z2gz.country_id = '0' OR z2gz.country_id = '" . (int)$country_id . "') AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$zone_id . "') " .
				" ORDER BY tr.priority ASC";
			$tax_rate_query = $this->db->query($sql);
			
			if($tax_rate_query->row){
				$rate = $tax_rate_query->row['rate'];
			}
		}
		
		return $rate;
	}
}
?>